package com.relytosoft.mizar.media.system.services.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.common.constant.MediaMqConstants;
import com.relytosoft.mizar.media.common.constant.PropertyConstants;
import com.relytosoft.mizar.media.common.emqx.client.MqttOp;
import com.relytosoft.mizar.media.system.api.entity.SysDistrict;
import com.relytosoft.mizar.media.system.api.entity.SysRelatedPlatform;
import com.relytosoft.mizar.media.system.api.pojo.dto.DistrictFeignDto;
import com.relytosoft.mizar.media.system.api.pojo.vo.DistrictSyncVo;
import com.relytosoft.mizar.media.system.services.service.DistrictSyncService;
import com.relytosoft.mizar.media.system.services.service.SysDistrictService;
import com.relytosoft.mizar.media.system.services.service.SysRelatedPlatformService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 区域同步
 *
 * @Author SunLi
 * @Date: 2022/4/8 10:08
 * @Version 1.0
 */
@Service
@ConditionalOnProperty(value = PropertyConstants.PROPERTY_MIZAR_MEDIA_STORETYPE, havingValue = "HK")
public class HKDistrictSyncServiceImpl implements DistrictSyncService {

    @Resource
    private SysRelatedPlatformService sysRelatedPlatformService;

    @Resource
    private SysDistrictService sysDistrictService;

//    @Resource
//    private KafkaProducer kafkaProducer;

    @Override
    public Integer insertDistrictSync(SysDistrict entity) {
        SysRelatedPlatform sysRelatedPlatform = new SysRelatedPlatform();
        sysRelatedPlatform.setPlatformName("HK");
        sysRelatedPlatform.setOwnId(entity.getId());
        sysRelatedPlatform.setIsSync(0);
        sysRelatedPlatform.setStatus(1);
        sysRelatedPlatform.setType(1);
        sysRelatedPlatformService.save(sysRelatedPlatform);
        return sysRelatedPlatform.getId();
    }

    @Override
    public void checkRootDistrictSync() {
        try {
            SysRelatedPlatform sysRelatedPlatform = sysRelatedPlatformService.getRelatedPlatform(1, "HK", 0, 1);
            if (sysRelatedPlatform == null) {
                //发送查询根区域消息
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("OpType","QUERYROOT");
                jsonObject.put("Data", "");
                MqttOp.publish(MediaMqConstants.REGION_TOPIC,JSON.toJSONString(jsonObject));
//                KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
//                kafkaOpMsg.setOpType("QUERYROOT");
//                kafkaOpMsg.setData("");
//                kafkaProducer.send(MediaMqConstants.REGION_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
            }
        } catch (Exception ex) {
        }
    }

    @Override
    public void districtListSync(List<DistrictSyncVo> sysDistrictList) {
        try {
            sysDistrictList.forEach(e -> {
                SysDistrict entity=new SysDistrict();
                entity.setName(e.getName());
                entity.setRemark(e.getRemark());
                entity.setRelatedPlatformId(e.getIdSync());
                entity.setParentPlatformId(e.getParentPlatformId());
                entity.setUpdatedId(CurrentUser.getCurrentUserId());
                entity.setUpdatedBy(CurrentUser.getCurrentUserFullName());
                //发送添加区域至第三方消息
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("OpType","INSERT");
                jsonObject.put("Data", entity);
                MqttOp.publish(MediaMqConstants.REGION_TOPIC,JSON.toJSONString(jsonObject));
//                KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
//                kafkaOpMsg.setOpType("INSERT");
//                kafkaOpMsg.setData(JSON.toJSONString(entity));
//                kafkaProducer.send(MediaMqConstants.REGION_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
            });
        } catch (Exception ex) {
        }
    }


    @Override
    public void districtSync(SysDistrict entity, Integer relatedPlatFormId) {
        try {
            //获取上级区域第三方关联信息
            SysRelatedPlatform sysRelatedPlatform = sysRelatedPlatformService.getRelatedPlatform(1, "HK", entity.getParentId(), 1);
            if (sysRelatedPlatform == null && entity.getParentId() == 0) {
                //根区域在第三方关联信息不存在
                //发送查询第三方根区域消息
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("OpType","QUERYROOT");
                jsonObject.put("Data", "");
                MqttOp.publish(MediaMqConstants.REGION_TOPIC,JSON.toJSONString(jsonObject));
//                KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
//                kafkaOpMsg.setOpType("QUERYROOT");
//                kafkaOpMsg.setData("");
//                kafkaProducer.send(MediaMqConstants.REGION_TOPIC, JSON.toJSONString(kafkaOpMsg), true);

            }
            if (relatedPlatFormId == null || relatedPlatFormId == 0) {
                SysRelatedPlatform curRelatedPlatform = sysRelatedPlatformService.getRelatedPlatform(1, "HK", entity.getId(), 0);
                if(curRelatedPlatform==null) {
                    return;
                }
                relatedPlatFormId = curRelatedPlatform.getId();
            }
            entity.setRelatedPlatformId(relatedPlatFormId);
            entity.setParentPlatformId(sysRelatedPlatform.getPlatformId());
            entity.setUpdatedId(CurrentUser.getCurrentUserId());
            entity.setUpdatedBy(CurrentUser.getCurrentUserFullName());
            //发送添加区域至第三方消息
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("OpType","INSERT");
            jsonObject.put("Data",entity);
            MqttOp.publish(MediaMqConstants.REGION_TOPIC,JSON.toJSONString(jsonObject));
//            KafkaOpMsg kafkaOpMsg = new KafkaOpMsg();
//            kafkaOpMsg.setOpType("INSERT");
//            kafkaOpMsg.setData(JSON.toJSONString(entity));
//            kafkaProducer.send(MediaMqConstants.REGION_TOPIC, JSON.toJSONString(kafkaOpMsg), true);
        } catch (Exception ex) {
        }
    }

    @Override
    public List<SysRelatedPlatform> selectRelatedPlatformList() {
        return sysRelatedPlatformService.selectRelatdPlatformList(1,"HK");
    }

    @Override
    public List<DistrictFeignDto> selectDistrictList() {
        return sysDistrictService.selectDistrictFeignList("HK");
    }
}
